﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// 脚本功能：公共 Panel 上按钮 的 控制
/// 添加对象：CommonBtnsCtr
/// </summary>

public class CommonBtnsCtr : MonoBehaviour {


    #region 数据定义和unity自带方法组
    private Button mBackBtn, mDataResetBtn, mResetBtn;

    public event OnClickBackBtn OnClickBackEvent;
    public delegate void OnClickBackBtn();
    public event OnClickDataResetBtn OnClickDataResetEvent;
    public delegate void OnClickDataResetBtn();
    public event OnClickResetBtn OnClickResetEvent;
    public delegate void OnClickResetBtn();

    private Vector2 dataBtnSizeVector;
    private Image mDataDoubleImage, mSingleImage;
    private Vector2 mDataResetBornPos, mResetBornPos, mCenterPos;
    void Awake()
    {
        FindGo();
        GetData();
        BindingEvent();
    }

    void Start()
    {
       
    }
    

    #endregion




    #region  私有方法组
    private void FindGo()
    {
        if (mBackBtn == null)
        {
            mBackBtn = TransformHelper.FindChild(transform, "BackBtn").GetComponent<Button>();
        }
        if (mResetBtn == null)
        {
            mResetBtn = TransformHelper.FindChild(transform, "ResetBtn").GetComponent<Button>();
        }
        if (mDataResetBtn == null)
        {
            mDataResetBtn = TransformHelper.FindChild(transform, "DataResetBtn").GetComponent<Button>();
        }
        if (mDataDoubleImage == null)
        {
            mDataDoubleImage = transform.Find("DataDoubleBg").GetComponent<Image>();
        }
        if (mSingleImage == null)
        {
            mSingleImage = transform.Find("DataSingleBg").GetComponent<Image>();
        }
    }

    private void GetData()
    {
        dataBtnSizeVector = (mDataResetBtn.transform as RectTransform).sizeDelta;
        mDataResetBornPos = (mDataResetBtn.transform as RectTransform).anchoredPosition;
        mResetBornPos = (mResetBtn.transform as RectTransform).anchoredPosition;
        mCenterPos = new Vector2(8.5f, 0);
    }
    private void BindingEvent()
    {
        if (mBackBtn != null)
        {
            mBackBtn.onClick.AddListener(delegate () { OnClickBackEvent(); });
        }
    }

    private void SetBgImageShow(bool isOnlyOne,bool allHide)
    {
        if (allHide)
        {
            mDataDoubleImage.gameObject.SetActive(false);
            mSingleImage.gameObject.SetActive(false);
            return;
        }
        mDataDoubleImage.gameObject.SetActive(!isOnlyOne);
        mSingleImage.gameObject.SetActive(isOnlyOne);
    }
    private void SetDataResetBtn(bool isShow,bool isOnlyOne, bool allHide = false)
    {
        SetBgImageShow(isOnlyOne, allHide);
        mDataResetBtn.gameObject.SetActive(isShow);
        (mDataResetBtn.transform as RectTransform).anchoredPosition = isOnlyOne ? mCenterPos : mDataResetBornPos;

        if (isShow )
        {
            mDataResetBtn.onClick.AddListener(delegate () { OnClickDataResetEvent(); });
        }
        else
        {
            mDataResetBtn.onClick.RemoveAllListeners();
        }
    }

    private void SetResetBtn(bool isShow, bool isOnlyOne, bool allHide = false)
    {
        SetBgImageShow(isOnlyOne, allHide);
        mResetBtn.gameObject.SetActive(isShow);
        (mResetBtn.transform as RectTransform).anchoredPosition = isOnlyOne ? mCenterPos : mResetBornPos;

        if (isShow)
        {
            mResetBtn.onClick.AddListener(delegate () { OnClickResetEvent(); });
        }
        else
        {
            mResetBtn.onClick.RemoveAllListeners();
        }
    }
    #endregion



    #region  公有方法组
    /// <summary>
    /// 控制 数据重置  和 重置 按钮的显示以及位置
    /// </summary>
    /// <param name="type">0都显示   1显示数据重置    2显示重置按钮  3都不显示</param>
    /// <param name="paramsObj">配置参数，用于以后的按钮位置拓展 todo</param>
    public void CommonBtnsShow(int type,params object[] paramsObj)
    {
        FindGo();
        if (paramsObj.Length > 0)
        {
            print("处理定制  方案？？");
        }
        else
        {
            switch (type)
            {
                case 0:
                    SetDataResetBtn(true, false);
                    SetResetBtn(true, false);
                    break;
                case 1:
                    SetDataResetBtn(true, true);
                    SetResetBtn(false, true);
                    break;
                case 2:
                    SetDataResetBtn(false, true);
                    SetResetBtn(true, true);
                    break;
                case 3:
                    SetDataResetBtn(false, false);
                    SetResetBtn(false, false, true);
                    break;
                default:
                    break;
            }
        }
    }
    #endregion

}
